#include "apue.h"

void abort(void) {
    struct sigaction action;
    sigset_t mask;
    pid_t pid;

    pid= getpid();

    /* Caller cant ignore SIGABRT, if so reset to default */
    if (sigaction(SIGABRT, NULL, &action) < 0) {
        err_sys("sigaction(SIGABRT) error");
    }
    if (action.sa_handler == SIG_IGN) {
        action.sa_handler= SIG_DFL; 
        sigaction(SIGABRT, &action, NULL);
    }
    if (action.sa_handler== SIG_DFL)
        fflush(NULL);   /* flush all open stdio streams */

    /* Caller cant block SIGABRT; make sure it's unblocked */
    sigfillset(&mask);
    sigdelset(&mask, SIGABRT);  /* mask has only SIGABRT turned off */
    sigprocmask(SIG_SETMASK, &mask, NULL);
    kill(pid, SIGABRT);

    /* If we're here, process caught SIGABRT and returned */
    fflush(NULL);

    /* for sig handled by kernel */ 
    action.sa_handler= SIG_DFL;
    sigaction(SIGABRT, &action, NULL);  /* reset to default */
    sigprocmask(SIG_SETMASK, &mask, NULL); /* just in case ... */
    kill(pid, SIGABRT);     /* and one more time */

    _exit(1);   /* this should never be executed ... */
}
